close all; clear; clc;
%% 2-1 图像预处理
figure(Name='The simulation window 1', NumberTitle='off');
original = imread("zyx.jpg");   % original是已经加载的图像变量
subplot(221);imshow(original); title('原始图像');

Gray_img = rgb2gray(original);      % 灰度图像
subplot(222);imshow(Gray_img); title('灰度图像');

Salt_Pepper_Image = imnoise(Gray_img, "salt & pepper", 0.02); %添加椒盐噪声
subplot(223);imshow(Salt_Pepper_Image); title('椒盐噪声图像');

Gaussian_Noise_Image = imnoise(Gray_img, "gaussian", 0.02); %添加高斯噪声
subplot(224);imshow(Gaussian_Noise_Image); title('高斯噪声图像');

%% 2-2 BOX模板
[height, width] = size(Salt_Pepper_Image);
BOX_Order = 3;  %Box模板阶数：3
BOX_Template = (1/BOX_Order/BOX_Order)*ones(BOX_Order); %模板
Salt_Pepper_Image = double(Salt_Pepper_Image);  %强制类型转换为double类型，为了 .* 运算
New_Image = zeros(height, width);   %创建一幅 全0矩阵

for ii = 2:height-1     % 注意边界
    for jj = 2:width-1
        New_Image(ii,jj) = sum(sum(Salt_Pepper_Image(ii-1:ii+1, jj-1:jj+1).*BOX_Template));  % 3*3区域 * BOX模板，加和
        % 越界处理
        if New_Image(ii,jj) > 255
            New_Image(ii,jj) = 255;
        elseif New_Image(ii,jj) <= 0
            New_Image(ii,jj) = 0;
        end
    end
end

%图像的边界 用原来的边界用原图像素
New_Image([1,end], 1:end) = Salt_Pepper_Image([1,end], 1:end);      %第一行和最后一行 所有像素点
New_Image([2:end-1], [1,end]) = Salt_Pepper_Image([2:end-1], [1,end]);  %第一列和最后一列 2~n-1个像素点

%一定要强制类型转换为uint8类型的数据，不然显示不出来人像，只有噪声
Salt_Pepper_Image = uint8(Salt_Pepper_Image);
New_Image = uint8(New_Image);

%显示图像
figure ('Name','The simulation window2', NumberTitle='off');
subplot(1,2,1); imshow(Salt_Pepper_Image), title('原始图像 - 椒盐噪声');     
subplot(1,2,2); imshow(New_Image),title('处理后的图像');

%% 2-3 不同大小的BOX模板

% 椒盐噪声图像 Salt_Pepper_Image
% 定义3x3的BOX模板（均值滤波器）
boxFilter3 = ones(3)/9;
boxFilter5 = ones(3)/25;
boxFilter7 = ones(3)/49;
% 使用卷积进行平滑处理，'same'参数保持输出图像大小与输入相同
% 'replicate'边界处理意味着边缘像素会被复制填充
smoothedImage3 = imfilter(Salt_Pepper_Image, boxFilter3, 'replicate');
smoothedImage5 = imfilter(Salt_Pepper_Image, boxFilter5, 'replicate');
smoothedImage7 = imfilter(Salt_Pepper_Image, boxFilter7, 'replicate');
% 创建一个新的窗口显示结果
figure('Name', 'The Simulation Window 3', NumberTitle='off');
% 显示原椒盐噪声图像
subplot(221);   imshow(Salt_Pepper_Image);  title('椒盐噪声图像');    axis off; % 关闭坐标轴显示

% 显示平滑处理后的图像
subplot(222);   imshow(smoothedImage3); title('3*3平滑处理后图像');    axis off;
subplot(223);   imshow(smoothedImage5); title('5*5平滑处理后图像');    axis off;
subplot(224);   imshow(smoothedImage7); title('7*7平滑处理后图像');    axis off;

%结论：BOX模板越大，处理后图像越暗

%% 2-4 3*3中值滤波
% 椒盐噪声图像 Salt_Pepper_Image
Model_Order = 3;  %Box模板阶数：3
medFiltered_Image = medianFilter(Salt_Pepper_Image, Model_Order); % 编程方式中值滤波
ImageFunctionCall = medfilt2(Salt_Pepper_Image, [Model_Order, Model_Order]);% 函数调用方式中值滤波
% 显示结果
figure('Name', 'The Simulation Window 4');
subplot(1, 3, 1);imshow(Salt_Pepper_Image);title('原图');
subplot(1, 3, 2);imshow(medFiltered_Image);title('编程中值滤波图像');
subplot(1, 3, 3);imshow(ImageFunctionCall);title('函数中值滤波图像');

M=imread('zyx.jpg');
N=rgb2gray(M);
N_noise_salt = imnoise(N,'salt & pepper',0.02); 
N_filtered11 = medfilt2(N_noise_salt,[3,3]);
n1 = 2; m1 = 2*n1+1;
n2 = 2; m2 = 2*n2+1;
k = floor(m1*m2/2)+1;
[h,l,c] = size(N_noise_salt);
H = zeros(h,l); 
t = zeros(n1,n2); 
for i=n1+1:h-n1
    for j=n2+1:l-n2
        Neiborhood = N_noise_salt(i-n1:i+n1,j-n2:j+n2);
        t = Neiborhood(:);
        s = sort(t);
        H(i,j) = s(k);
    end
end
N_filtered2 = uint8(H);
subplot(1,3,1),imshow(N_noise_salt);title('原图');
subplot(1,3,2),imshow(N_filtered11);title('函数中值滤波图像');
subplot(1,3,3),imshow(N_filtered2);title('编程中值滤波图像');

